/*
 * @(#)MtmWorkflowStepWorkflowStep.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep;
import com.dcivision.workflow.bean.WorkflowStep;

/**
 * MtmWorkflowStepWorkflowStepDAObject.java
 *
 * This class is the data access bean for table "MTM_WF_STEP_WF_STEP".
 *
 * @author      Angus Shiu
 * @company     DCIVision Limited
 * @creation date   05/08/2003
 * @version     $Revision: 1.35 $
*/

public class MtmWorkflowStepWorkflowStepDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.35 $";

  public static final String TABLE_NAME = "MTM_WF_STEP_WF_STEP";

  public MtmWorkflowStepWorkflowStepDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("PARENT_STEP_ID");
    this.vecDBColumn.add("PARENT_TYPE");
    this.vecDBColumn.add("GROUP_ID");
    this.vecDBColumn.add("CHILD_STEP_ID");
    this.vecDBColumn.add("LOOP_BACK");
    this.vecDBColumn.add("LOOP_BACK_ALL_BRANCH");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);      
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          return(tmpMtmWorkflowStepWorkflowStep);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          tmpMtmWorkflowStepWorkflowStep.setRecordCount(totalNumOfRecord);
          tmpMtmWorkflowStepWorkflowStep.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = (MtmWorkflowStepWorkflowStep)((MtmWorkflowStepWorkflowStep)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();

        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   MTM_WF_STEP_WF_STEP(ID, PARENT_STEP_ID, PARENT_TYPE, GROUP_ID, CHILD_STEP_ID, LOOP_BACK, LOOP_BACK_ALL_BRANCH, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpMtmWorkflowStepWorkflowStep.getParentStepID());
        setPrepareStatement(preStat, 3, tmpMtmWorkflowStepWorkflowStep.getParentType());
        setPrepareStatement(preStat, 4, tmpMtmWorkflowStepWorkflowStep.getGroupID());
        setPrepareStatement(preStat, 5, tmpMtmWorkflowStepWorkflowStep.getChildStepID());
        setPrepareStatement(preStat, 6, tmpMtmWorkflowStepWorkflowStep.getLoopBack());
        setPrepareStatement(preStat, 7, tmpMtmWorkflowStepWorkflowStep.getLoopBackAllBranch());
        setPrepareStatement(preStat, 8, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 9, new Integer(0));
        setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 11, currTime);
        setPrepareStatement(preStat, 12, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 13, currTime);
        preStat.executeUpdate();
        tmpMtmWorkflowStepWorkflowStep.setID(nextID);
        tmpMtmWorkflowStepWorkflowStep.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmWorkflowStepWorkflowStep.setCreateDate(currTime);
        tmpMtmWorkflowStepWorkflowStep.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmWorkflowStepWorkflowStep.setUpdateDate(currTime);
        tmpMtmWorkflowStepWorkflowStep.setUpdateCount(new Integer(0));
        tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
        tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
        tmpMtmWorkflowStepWorkflowStep.setRecordStatus(GlobalConstant.RECORD_STATUS_ACTIVE);//simon
        return(tmpMtmWorkflowStepWorkflowStep);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = (MtmWorkflowStepWorkflowStep)((MtmWorkflowStepWorkflowStep)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_WF_STEP_WF_STEP ");
        sqlStat.append("SET  PARENT_STEP_ID=?, PARENT_TYPE=?, GROUP_ID=?, CHILD_STEP_ID=?, LOOP_BACK=?, LOOP_BACK_ALL_BRANCH=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWorkflowStepWorkflowStep.getParentStepID());
        setPrepareStatement(preStat, 2, tmpMtmWorkflowStepWorkflowStep.getParentType());
        setPrepareStatement(preStat, 3, tmpMtmWorkflowStepWorkflowStep.getGroupID());
        setPrepareStatement(preStat, 4, tmpMtmWorkflowStepWorkflowStep.getChildStepID());
        setPrepareStatement(preStat, 5, tmpMtmWorkflowStepWorkflowStep.getLoopBack());
        setPrepareStatement(preStat, 6, tmpMtmWorkflowStepWorkflowStep.getLoopBackAllBranch());
        setPrepareStatement(preStat, 7, new Integer(tmpMtmWorkflowStepWorkflowStep.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat, 10, tmpMtmWorkflowStepWorkflowStep.getID());
        setPrepareStatement(preStat, 11, tmpMtmWorkflowStepWorkflowStep.getUpdateCount());
        log.info("sqlStat="+sqlStat);//simon_add
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(currTime);
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(new Integer(tmpMtmWorkflowStepWorkflowStep.getUpdateCount().intValue() + 1));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          return(tmpMtmWorkflowStepWorkflowStep);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = (MtmWorkflowStepWorkflowStep)((MtmWorkflowStepWorkflowStep)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWorkflowStepWorkflowStep.getID());
        setPrepareStatement(preStat, 2, tmpMtmWorkflowStepWorkflowStep.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmWorkflowStepWorkflowStep);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = (MtmWorkflowStepWorkflowStep)this.oldValue;

    if (tmpMtmWorkflowStepWorkflowStep != null) {
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getParentStepID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getParentType()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getGroupID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getChildStepID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getLoopBack()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getUpdateDate()));
    }

    tmpMtmWorkflowStepWorkflowStep = (MtmWorkflowStepWorkflowStep)obj;
    if (tmpMtmWorkflowStepWorkflowStep != null) {
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getParentStepID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getParentType()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getGroupID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getChildStepID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getLoopBack()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowStepWorkflowStep.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  /**
   * Gets all the parent-child step relationship of the given workflow record
   * 
   * @param workflowRecordID The workflow record ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowRecord
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */
  public synchronized List getListByWorkflowID(Integer workflowRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT   DISTINCT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM     MTM_WF_STEP_WF_STEP A, WORKFLOW_STEP B ");
        sqlStat.append("WHERE    A.RECORD_STATUS = ? AND ");
        sqlStat.append("         (B.ID = A.PARENT_STEP_ID OR B.ID = A.CHILD_STEP_ID) AND ");
        sqlStat.append("         B.WORKFLOW_RECORD_ID = ? ");
        sqlStat.append("ORDER BY A.PARENT_STEP_ID ASC, A.GROUP_ID ASC ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowRecordID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of parent-child relation of the given parent step and child step 
   * 
   * @param parentID The parent step ID
   * @param childID The child step ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowRecord
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */
  public synchronized List getListByParentIDAndChildID(Integer parentID, Integer childID) throws ApplicationException {
    return getListByParentIDChildIDAndParentType(parentID, childID, null);
  }
  /**
   * Gets a list of parent-child relation of the given parent step and child step 
   * and parent type
   * 
   * @param parentID The parent step ID
   * @param childID The child step ID
   * @param parentType The parent type, null if ignore
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowRecord
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_APPROVED
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_FROM_VALUE_DRIVEN
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_NO
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_REJECTED
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_ROUTINE
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_SINGLE
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_YES
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */ 
  public synchronized List getListByParentIDChildIDAndParentType(Integer parentID, Integer childID, String parentType) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.PARENT_STEP_ID = ? ");
        if (!Utility.isEmpty(childID)) {
          sqlStat.append("AND A.CHILD_STEP_ID = ? ");
        }
        if (!Utility.isEmpty(parentType)) {
          sqlStat.append("AND A.PARENT_TYPE = ? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, parentID);
        int k=3;
        if (!Utility.isEmpty(childID)) {
          this.setPrepareStatement(preStat, k++, childID);
        }
        if (!Utility.isEmpty(parentType)) {
          this.setPrepareStatement(preStat, k++, parentType);
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of parent-child relation that is belongs to the same group for a given workflow record
   * 
   * @param workflowRecordID The workflow record ID
   * @param groupID The merge group ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowRecord
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */
  public synchronized List getListUnderSameGroupByWorkflowRecordIDAndGroupID(Integer workflowRecordID, Integer groupID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A, WORKFLOW_STEP B ");
        sqlStat.append("WHERE  A.PARENT_STEP_ID = B.ID AND A.RECORD_STATUS = ? AND B.WORKFLOW_RECORD_ID = ? AND A.GROUP_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowRecordID);
        this.setPrepareStatement(preStat, 3, groupID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets the next merge group ID for a given workflow record
   * 
   * @param workflowRecordID The workflow record ID
   * @return The next group ID
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowRecord
   */
  public synchronized Integer getNextGroupIDForWorkflowRecord(Integer workflowRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    Integer nextGroupID = null;

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT MAX(A.GROUP_ID)+1 AS NEXT_GROUP_ID ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A, WORKFLOW_STEP B ");
        sqlStat.append("WHERE  A.PARENT_STEP_ID = B.ID AND A.RECORD_STATUS = ? AND B.WORKFLOW_RECORD_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowRecordID);
        rs = preStat.executeQuery();
        if (rs.next()) {
          nextGroupID = getResultSetInteger(rs, "NEXT_GROUP_ID");
        }
        return(nextGroupID);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a parent-end relation of the parent step that is directly connected to
   * a End step with the specific parent type
   * 
   * @param workflowRecordID The workflow record ID of the parent step
   * @param parentType The parent type 
   * @param parentStepID The workflow step ID
   * @return The parent-end relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */
  public synchronized AbstractBaseObject getParentStepToEndStepRecord(Integer workflowRecordID, String parentType, Integer parentStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    Integer nextGroupID = null;

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A, WORKFLOW_STEP B ");

        /*
        sqlStat.append("WHERE  A.CHILD_STEP_ID = B.ID AND A.RECORD_STATUS = ? AND B.WORKFLOW_RECORD_ID = ? AND A.PARENT_STEP_ID = ? AND B.STEP_SEQ = ? AND A.PARENT_TYPE = ?");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowRecordID);
        this.setPrepareStatement(preStat, 3, parentStepID);
        this.setPrepareStatement(preStat, 4, WorkflowStep.END_STEP_SEQ_NO);
        this.setPrepareStatement(preStat, 5, parentType);
        */

        sqlStat.append(" WHERE  A.CHILD_STEP_ID = B.ID ");
        sqlStat.append(" AND A.RECORD_STATUS = '" + GlobalConstant.RECORD_STATUS_ACTIVE +"' ");
        sqlStat.append(" AND B.WORKFLOW_RECORD_ID = " + workflowRecordID.intValue() );
        sqlStat.append(" AND A.PARENT_STEP_ID = " + parentStepID.intValue());
        sqlStat.append(" AND B.STEP_SEQ = " + WorkflowStep.END_STEP_SEQ_NO.intValue() );
        sqlStat.append(" AND A.PARENT_TYPE = '" + parentType + "' ");
        preStat = dbConn.prepareStatement(sqlStat.toString());

        rs = preStat.executeQuery();

        if (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          return (tmpMtmWorkflowStepWorkflowStep);
        } else {
          return null;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a parent-child relation of the parent step that is directly connected to
   * the child step with the specific parent type
   * 
   * @param workflowRecordID The workflow record ID of the parent step
   * @param parentStepID The parent workflow step ID
   * @param parentType The parent type 
   * @param childStepID The child workflow step ID
   * @return The parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */
  public synchronized AbstractBaseObject getParentStepToChildStepRecord(Integer workflowRecordID, Integer parentStepID,String parentType,Integer childStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    Integer nextGroupID = null;

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
        sqlStat.append(" WHERE  A.PARENT_STEP_ID = " + parentStepID.intValue());
        sqlStat.append(" AND  A.CHILD_STEP_ID = " + childStepID.intValue());
        if(!Utility.isEmpty(parentType)){//TC.ADD for some time did not need parentType
          sqlStat.append(" AND A.PARENT_TYPE = '" + parentType + "' ");
        }
        sqlStat.append(" AND A.RECORD_STATUS = '" + GlobalConstant.RECORD_STATUS_ACTIVE +"' ");
        //sqlStat.append(" AND B.WORKFLOW_RECORD_ID = " + workflowRecordID.intValue() );
        //sqlStat.append(" AND A.PARENT_STEP_ID = " + parentStepID.intValue());
        //sqlStat.append(" AND B.STEP_SEQ = " + WorkflowStep.END_STEP_SEQ_NO.intValue() );
        //sqlStat.append(" AND A.PARENT_TYPE = '" + parentType + "' ");
        preStat = dbConn.prepareStatement(sqlStat.toString());

        rs = preStat.executeQuery();

        if (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          return (tmpMtmWorkflowStepWorkflowStep);
        } else {
          return null;
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Deletes the given parent-child relation from database
   * 
   * @param obj The parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */
  public synchronized void deleteUnusedObject(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = (MtmWorkflowStepWorkflowStep)((MtmWorkflowStepWorkflowStep)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWorkflowStepWorkflowStep.getID());
        setPrepareStatement(preStat, 2, tmpMtmWorkflowStepWorkflowStep.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of parent-child relation of the given workflow record and child step ID
   * of the same merge group ID
   * 
   * @param workflowRecordID The workflow record ID
   * @param workflowGroupID The merge group ID
   * @param childID The child workflow step ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */
  public synchronized List getListByWorkflowRecordIDGroupIDAndChildID(Integer workflowRecordID, Integer groupID, Integer childID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A, WORKFLOW_STEP B ");
        sqlStat.append("WHERE  A.CHILD_STEP_ID = B.ID AND A.RECORD_STATUS = ? ");
        sqlStat.append("       AND B.WORKFLOW_RECORD_ID = ? AND A.CHILD_STEP_ID = ? ");
        
        if(!Utility.isEmpty(groupID)){
            sqlStat.append("       AND A.GROUP_ID = ? ");  
        }
        
        preStat = dbConn.prepareStatement(sqlStat.toString());
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowRecordID);        
        this.setPrepareStatement(preStat, 3, childID);
        
        if(!Utility.isEmpty(groupID)){
        	this.setPrepareStatement(preStat, 4, groupID);
        }
        
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Deletes the parent-child relation of the given parent step and/or the loopback setting
   * 
   * @param parentStepID The parent step ID
   * @param loopBack The loopback options, Y/N
   * @throws ApplicationException
   * @see com.dcivision.framework.GlobalConstant#TRUE
   * @see com.dcivision.framework.GlobalConstant#FALSE
   * @see com.dcivision.workflow.bean.WorkflowStep
   */
  public void deleteListByParentStepIDLoopBackType(Integer parentStepID, String loopBack) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP ");
        sqlStat.append("WHERE  PARENT_STEP_ID=? ");
        if (loopBack!=null) {
          sqlStat.append("AND LOOP_BACK=? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, parentStepID);
        if (loopBack!=null) {
          setPrepareStatement(preStat, 2, loopBack);
        }
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of parent-child relation of the given parent step
   * 
   * @param parentStepID The parent step ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */ 
  public synchronized List getListByParentStepID(Integer parentStepID) throws ApplicationException {
    return getListByParentStepIDAndParentType(parentStepID, null);//simon4_this.getListByParentStepIDAndParentType()
  }
  /**
   * Gets a list of parent-child relation of the given parent step and parent type
   * 
   * @param parentStepID The parent step ID
   * @param parentType The parent type, null if ignore
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowRecord
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_APPROVED
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_FROM_VALUE_DRIVEN
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_NO
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_REJECTED
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_ROUTINE
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_SINGLE
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_YES
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */ 
  public synchronized List getListByParentStepIDAndParentType(Integer parentStepID, String parentType) throws ApplicationException {
    PreparedStatement preStat = null;//simon4_this.getListByParentStepIDAndParentType()
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A \n");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? \n");
        sqlStat.append("  AND A.PARENT_STEP_ID = ? \n");
        if ( !Utility.isEmpty(parentType) ) {
          sqlStat.append("  AND A.PARENT_TYPE = ? \n");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, parentStepID);
        if ( !Utility.isEmpty(parentType) ) {
          this.setPrepareStatement(preStat, 3, parentType);
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));         
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));         
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));         
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));         
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));         
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));         
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  //simon----start
  /**
   * Gets a list of parent-child relation of the given parent step
   * 
   * @param parentStepID The parent step ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */ 
  public synchronized List getList_ByParentStepIDAndParentType_InStepAndStep(Integer parentStepID) throws ApplicationException {
    return getList_ByParentStepIDAndParentType_InStepAndStep(parentStepID, null);
  }
  public synchronized List getList_ByParentStepIDAndParentType_InStepAndStep(Integer parentStepID, String parentType) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A \n");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? \n");
        sqlStat.append("  AND A.PARENT_STEP_ID = ? \n");
        if ( !Utility.isEmpty(parentType) ) {
          sqlStat.append("  AND A.PARENT_TYPE = ? \n");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, parentStepID);
        if ( !Utility.isEmpty(parentType) ) {
          this.setPrepareStatement(preStat, 3, parentType);
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));         
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));         
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));         
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));         
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));         
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));         
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of parent-child relation of the given parent step
   * 
   * @param parentStepID The parent step ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */ 
  public synchronized List getList_ByChildStepIDAndParentType_InStepAndStep(Integer ChildStepID) throws ApplicationException {
    return getList_ByChildStepIDAndParentType_InStepAndStep(ChildStepID, null);//simon4_this.getListByParentStepIDAndParentType()
  }
  /**
   * Gets a list of parent-child relation of the given parent step and parent type
   * 
   * @param parentStepID The parent step ID
   * @param parentType The parent type, null if ignore
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowRecord
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_APPROVED
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_FROM_VALUE_DRIVEN
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_NO
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_REJECTED
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_ROUTINE
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_SINGLE
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_YES
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */ 
  public synchronized List getList_ByChildStepIDAndParentType_InStepAndStep(Integer ChildStepID, String parentType) throws ApplicationException {
    PreparedStatement preStat = null;//simon4_this.getListByParentStepIDAndParentType()
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A \n");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? \n");
        sqlStat.append("  AND A.CHILD_STEP_ID = ? \n");
        if ( !Utility.isEmpty(parentType) ) {
          sqlStat.append("  AND A.PARENT_TYPE = ? \n");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, ChildStepID);
        if ( !Utility.isEmpty(parentType) ) {
          this.setPrepareStatement(preStat, 3, parentType);
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));         
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));         
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));         
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));         
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));         
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));         
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of parent-child relation of the given child step
   * 
   * @param childStepID The child step ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */
  public synchronized List getListByParentStepIDAndChildStepID(Integer parentStepID,Integer childStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
        sqlStat.append("WHERE  A.PARENT_STEP_ID = ? AND A.CHILD_STEP_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, parentStepID);
        this.setPrepareStatement(preStat, 2, childStepID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  //simon----end
  /**
   * Gets a list of parent-child relation of the given child step
   * 
   * @param childStepID The child step ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */ 
  public synchronized List getListByChildStepID(Integer childStepID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.CHILD_STEP_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, childStepID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets the parent-child relation of the given child step and the loopback setting
   * 
   * @param childStepID The child step ID
   * @param loopBack The loopback options, Y/N
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.framework.GlobalConstant#TRUE
   * @see com.dcivision.framework.GlobalConstant#FALSE
   * @see com.dcivision.workflow.bean.WorkflowStep
   */
  public synchronized List getListByChildStepIDAndLoopBack(Integer childStepID, String loopBack) throws ApplicationException {
      PreparedStatement preStat = null;
      ResultSet rs = null;
      StringBuffer sqlStat = new StringBuffer();
      List result = new ArrayList();

      synchronized(dbConn) {
        try {
          sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
          sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
          sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.CHILD_STEP_ID = ? AND A.LOOP_BACK = ? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
          this.setPrepareStatement(preStat, 2, childStepID);
          this.setPrepareStatement(preStat, 3, loopBack);
          rs = preStat.executeQuery();
          while (rs.next()) {
            MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
            tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
            tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
            tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
            tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
            tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
            tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
            tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
            tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
            tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
            tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
            tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
            tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
            tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
            tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
            tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
            result.add(tmpMtmWorkflowStepWorkflowStep);
          }
          return(result);
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
    }
  
  public synchronized List getListByChildStepIDAndLoopBackAndGroupID(Integer childStepID, String loopBack,Integer groupID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.CHILD_STEP_ID = ? AND A.LOOP_BACK = ? AND A.GROUP_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, childStepID);
        this.setPrepareStatement(preStat, 3, loopBack);
        this.setPrepareStatement(preStat, 4, groupID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of parent-child relation of the given parent step and parent type
   * 
   * @param parentStepID The parent step ID
   * @param parentType The parent type, null if ignore
   * @param loopBack The loopback options, Y/N
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.framework.GlobalConstant#TRUE
   * @see com.dcivision.framework.GlobalConstant#FALSE
   * @see com.dcivision.workflow.bean.WorkflowRecord
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_APPROVED
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_FROM_VALUE_DRIVEN
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_NO
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_REJECTED
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_ROUTINE
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_SINGLE
   * @see com.dcivision.workflow.bean.WorkflowStep#PARENT_TYPE_YES
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */ 
  public synchronized List getListByParentStepIDAndParentTypeAndLoopBack(Integer parentStepID, String parentType, String loopBack) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.PARENT_STEP_ID, A.PARENT_TYPE, A.GROUP_ID, A.CHILD_STEP_ID, A.LOOP_BACK, A.LOOP_BACK_ALL_BRANCH, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.PARENT_STEP_ID = ? AND A.PARENT_TYPE = ? AND A.LOOP_BACK = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, parentStepID);
        this.setPrepareStatement(preStat, 3, parentType);
        this.setPrepareStatement(preStat, 4, loopBack);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowStepWorkflowStep tmpMtmWorkflowStepWorkflowStep = new MtmWorkflowStepWorkflowStep();
          tmpMtmWorkflowStepWorkflowStep.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentStepID(getResultSetInteger(rs, "PARENT_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setParentType(getResultSetString(rs, "PARENT_TYPE"));
          tmpMtmWorkflowStepWorkflowStep.setGroupID(getResultSetInteger(rs, "GROUP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setChildStepID(getResultSetInteger(rs, "CHILD_STEP_ID"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBack(getResultSetString(rs, "LOOP_BACK"));
          tmpMtmWorkflowStepWorkflowStep.setLoopBackAllBranch(getResultSetString(rs, "LOOP_BACK_ALL_BRANCH"));
          tmpMtmWorkflowStepWorkflowStep.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowStepWorkflowStep.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowStepWorkflowStep.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowStepWorkflowStep.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getCreatorID()));
          tmpMtmWorkflowStepWorkflowStep.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowStepWorkflowStep.getUpdaterID()));
          result.add(tmpMtmWorkflowStepWorkflowStep);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Deletes all the parent-step relation of the given workflow record
   * 
   * @param workflowRecordID The workflow record ID
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowStep
   */
  public void deleteListByWorkflowRecordID(Integer workflowRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    PreparedStatement preStat2 = null;
    ResultSet rs2 = null;
    StringBuffer sqlStat2 = new StringBuffer();
    List result2 = new ArrayList();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat2.append("SELECT ID FROM WORKFLOW_STEP ");
        sqlStat2.append("WHERE WORKFLOW_RECORD_ID = "+workflowRecordID.toString()+" ");
        sqlStat2.append("AND RECORD_STATUS = '"+GlobalConstant.RECORD_STATUS_ACTIVE+"' ");
        preStat2 = dbConn.prepareStatement(sqlStat2.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs2 = preStat2.executeQuery();
        String stepID = "";
        while (rs2.next()) {
            stepID += ((stepID.length()>0)?",":"") + getResultSetInteger(rs2, "ID");
        }
        //null replaced with "-1"
        if(Utility.isEmpty(stepID)){
            stepID="-1";
        }
        sqlStat.append("DELETE ");
        sqlStat.append("FROM MTM_WF_STEP_WF_STEP ");
        sqlStat.append("WHERE RECORD_STATUS = ? ");
        sqlStat.append("AND PARENT_STEP_ID IN ("+stepID+") ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        updateCnt = preStat.executeUpdate();
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Deletes the steps of the given workflow record and step sequence number
   * 
   * @param workflowRecordID The workflow record ID
   * @param stepSeq Array of step sequence number to be deleted
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowRecord
   * @see com.dcivision.workflow.bean.WorkflowStep
   */
  public void deleteUnusedStepsByWorkflowRecordID(Integer workflowRecordID, String[] stepSeq) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    PreparedStatement preStat2 = null;
    ResultSet rs2 = null;
    StringBuffer sqlStat2 = new StringBuffer();
    List result2 = new ArrayList();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        String stepSeqs = "";
        for(int i=0;i<stepSeq.length;i++) {
          stepSeqs += ((stepSeqs.length()>0)?",":"") + stepSeq[i];
        }
        //null replaced with "-1"
        if(Utility.isEmpty(stepSeqs)){
          stepSeqs="-1";
        }
        sqlStat.append("DELETE ");
        sqlStat.append("FROM WORKFLOW_STEP ");
        sqlStat.append("WHERE RECORD_STATUS = ? ");
        sqlStat.append("AND WORKFLOW_RECORD_ID = ? ");
        sqlStat.append("AND STEP_SEQ NOT IN ("+stepSeqs+") ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 2, workflowRecordID);
        updateCnt = preStat.executeUpdate();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * Gets the largest merge group ID of a given workflow record
   * 
   * @param workflowRecordID The workflow record ID
   * @return The largest merge group ID
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowRecord
   * @see com.dcivision.workflow.bean.WorkflowStep
   */
  public synchronized Integer getMaxGroupIDByWorkflowRecordID(Integer workflowRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT MAX(A.GROUP_ID) GROUP_ID ");
        sqlStat.append("FROM   MTM_WF_STEP_WF_STEP A, WORKFLOW_RECORD B, WORKFLOW_STEP C \n");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? \n");    
        sqlStat.append("  AND B.ID = ? \n");
        sqlStat.append("  AND A.PARENT_STEP_ID = C.ID \n");
        sqlStat.append("  AND B.ID = C.WORKFLOW_RECORD_ID \n");
        sqlStat.append("  AND A.RECORD_STATUS = B.RECORD_STATUS \n");
        sqlStat.append("  AND A.RECORD_STATUS = C.RECORD_STATUS \n");
        
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowRecordID);
        
        rs = preStat.executeQuery();
        while (rs.next()) {
          return getResultSetInteger(rs, "GROUP_ID");          
        }
        return new Integer(0);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * Gets a list of parent-child relation of the given workflow record and child step ID
   * 
   * @param workflowRecordID The workflow record ID
   * @param childID The child workflow step ID
   * @return List of parent-child relation
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.MtmWorkflowStepWorkflowStep
   */
  public List getListByWorkflowRecordIDAndChildID(Integer workflowRecordID, Integer childID) throws ApplicationException {
    return this.getListByWorkflowRecordIDGroupIDAndChildID(workflowRecordID, null, childID);
  }
}
